﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Text;
using DDU.Infrastructure;
using DDU.Enums;
using DDU.Library.Helpers;
using Microsoft.Phone.Controls;
using System.Windows;
using System.ComponentModel;
using System.Windows.Media;
using DDU.Library.Extensions;
using System.IO.IsolatedStorage;
using DDU.DataModel;
using Microsoft.Phone.Net.NetworkInformation;

namespace DDU.ViewModel
{
    public class ViewModelBase : NotificationObject
    {
        public IDataContext Database;

        public ViewModelBase()
        {
            Database = new IDataContext(ConstKeys.ConnectionString);
            if (!IsDesignMode)
            {
                CheckBooksImageDirectory();
                GlobalSettings.SettingsChanged -= GlobalSettings_SettingsChanged;
                GlobalSettings.SettingsChanged += GlobalSettings_SettingsChanged;
                DeviceNetworkInformation.NetworkAvailabilityChanged += DeviceNetworkInformation_NetworkAvailabilityChanged;
            }
        }

        public virtual void Submit()
        {
            Database.SubmitChanges(ConflictMode.FailOnFirstConflict);
        }

        public virtual void Navigate(Uri uri)
        {
            PhoneApplicationFrame frame = (PhoneApplicationFrame)Application.Current.RootVisual;
            frame.Navigate(uri);
        }

        public virtual void RecordData(Int64 length)
        {
            Int64 original = 0;
            if (NetworkType == NetworkInterfaceType.Wireless80211)
            {
                original = GlobalSettings.WiFiNetworkDataLength + length;
                GlobalSettings.WiFiNetworkDataLength = original;
            }
            else
            {
                original = GlobalSettings.CellNetworkDataLength + length;
                GlobalSettings.CellNetworkDataLength = original;
            }
            GlobalSettings.Save();
        }

        #region EnumsCollections
        public IEnumerable<EducationLevelValue> EnumsOfEducationLevel
        {
            get
            {
                return EnumHelper.GetEnumValues<EducationLevelValue>();
            }
        }

        public IEnumerable<DayOfWeek> EnumsOfDays
        {
            get
            {
                return EnumHelper.GetEnumValues<DayOfWeek>();
            }
        }

        public IEnumerable<HalfTermValue> EnumsOfHalfTerm
        {
            get
            {
                return EnumHelper.GetEnumValues<HalfTermValue>();
            }
        }

        public IEnumerable<UnlockPaymentWayValue> EnumsUnlockPaymentWay
        {
            get
            {
                return EnumHelper.GetEnumValues<UnlockPaymentWayValue>();
            }
        }
        #endregion

        #region CommonProperties
        public bool FeatureAvailable
        {
            get
            {
                return !GlobalSettings.AdvancedLocked || ConstKeys.Edition == EditionValue.Premium;
            }
        }

        public Uri BackUri
        {
            get
            {
                PhoneApplicationFrame frame = (PhoneApplicationFrame)Application.Current.RootVisual;
                PhoneApplicationPage page = (PhoneApplicationPage)frame.Content;
                if (page.Tag != null)
                {
                    return (Uri)page.Tag;
                }
                return null;
            }
        }

        public bool IsDesignMode
        {
            get
            {
                return DesignerProperties.IsInDesignTool;//(bool)DependencyPropertyDescriptor.FromProperty(DesignerProperties.IsInDesignModeProperty, typeof(FrameworkElement)).Metadata.DefaultValue;
            }
        }

        public bool IsNetworkAvailable
        {
            get
            {
                return DeviceNetworkInformation.IsNetworkAvailable;
            }
        }

        private NetworkInterfaceType networkType = NetworkInterface.NetworkInterfaceType;
        public NetworkInterfaceType NetworkType
        {
            get
            {
                return networkType;
            }
            set
            {
                networkType = value;
                RaisePropertyChanged(() => NetworkType);
            }
        }

        public bool IsWiFiAvailable
        {
            get
            {
                return DeviceNetworkInformation.IsWiFiEnabled;
            }
        }

        public string LogoPath
        {
            get
            {
                Visibility visibility = Application.Current.FindResource<Visibility>("PhoneDarkThemeVisibility");
                if (visibility == Visibility.Visible)
                {
                    return "/DDU.Curriculum;component/Assets/Logo_Light.png";
                }
                return "/DDU.Curriculum;component/Assets/Logo_Dark.png";
            }
        }

        private OptionInfo globalSettings = new OptionInfo();
        public OptionInfo GlobalSettings
        {
            get { return globalSettings; }
            set
            {
                globalSettings = value;
                RaisePropertyChanged(() => GlobalSettings);
            }
        }
        #endregion

        #region Preload
        private void CheckBooksImageDirectory()
        {
            using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (!store.DirectoryExists(ConstKeys.BookDirectoryName + "\\"))
                {
                    store.CreateDirectory(ConstKeys.BookDirectoryName);
                }

                if (!store.DirectoryExists(ConstKeys.FictionsDirectoryName + "\\"))
                {
                    store.CreateDirectory(ConstKeys.FictionsDirectoryName);
                }

                if (!store.DirectoryExists(ConstKeys.VacationDirectoryName + "\\"))
                {
                    store.CreateDirectory(ConstKeys.VacationDirectoryName);
                }

                if (!store.DirectoryExists(ConstKeys.LanguagesDirectoryName + "\\"))
                {
                    store.CreateDirectory(ConstKeys.LanguagesDirectoryName);
                }
            }
        }
        #endregion

        #region Events
        void DeviceNetworkInformation_NetworkAvailabilityChanged(object sender, NetworkNotificationEventArgs e)
        {
            //IsNetworkAvailable = e.NetworkInterface.InterfaceState == ConnectState.Connected;
            //NetworkInterfaceList

            //IsWiFiAvailable = e.NetworkInterface.InterfaceSubtype == NetworkInterfaceSubType.WiFi;
            NetworkType = e.NetworkInterface.InterfaceType;
            RaisePropertyChanged(() => NetworkType);
            RaisePropertyChanged(() => IsWiFiAvailable);
            RaisePropertyChanged(() => IsNetworkAvailable);
        }

        void GlobalSettings_SettingsChanged(object sender, EventArgs e)
        {
            GlobalSettings.Save();
        }
        #endregion
    }
}
